package com.qyhl.webtv.module_user.setting.userinfo;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.user.UserInfoBean;
import com.qyhl.webtv.module_user.common.UserUrl;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by qyhl on 2017/12/8.
 */

public class UserInfoModel implements UserInfoContract.UserInfoModel {
    private UserInfoPresenter mPresenter;

    public UserInfoModel(UserInfoPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 上传头像
     *
     * @param logo
     */
    @Override
    public void upLoadHead(String logo) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(UserUrl.GET_HEAD_ICON)
                .params("username", userName)
                .params("logo", logo)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        mPresenter.headError("网络异常，头像上传失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s, ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.headSuccess();
                        } else {
                            mPresenter.headError("解析出错，头像上传失败！");
                        }
                    }
                });

    }

    /**
     * 修改信息
     *
     * @param sex
     * @param email
     * @param nickName
     */
    @Override
    public void changeInfo(String sex, String email, String nickName) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(UserUrl.GET_CHANGE_INFO)
                .params("username", userName)
                .params("sex", sex)
                .params("email", email)
                .params("nickname", nickName)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.changeError("网络异常，修改信息失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s, ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.changeSuccess();
                        } else {
                            mPresenter.changeError("网络异常，修改信息失败！");
                        }
                    }
                });
    }

    /**
     * 获取用户信息
     *
     * @param username
     */
    @Override
    public void getUserInfo(String username) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = UserUrl.GET_USER_INFO_NEW + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&username=" + username))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.userInfoError("网络异常，用户信息更新失败！");
                    }

                    @Override
                    public void onSuccess(UserInfoBean userInfoBean) {
                        mPresenter.userInfoSuccess(userInfoBean);
                    }
                });
    }
}
